/*
 * @Author: 杨三炮
 * @Date: 2021-11-19 13:18:21
 * @LastEditors: 杨三炮
 * @LastEditTime: 2021-11-24 16:06:50
 * @FilePath: \node\node\dao\admin_dao.js
 */

module.exports = class admin_dao extends require('../model/admin_mod') {
    /**
     * @description: 根据用户类型与查询字段模糊查询
     * @param {*} req
     * @param {*} res
     * @return {*} (数据与总数量返回)
     */
    static async getUserByTypeAndChar (req, res) {

        let data = {
            inputText: req.query.inputText,
            CharType: req.query.CharType,
            type: req.query.type
        }
        try {
            let getData = await this.getUserByTypeAndCharMod(data)
            if (!getData.length) {
                res.json({
                    msg: '没有数据',
                    code: 205,
                    data: getData,
                })
                return
            }
            res.json({
                msg: '获取成功',
                code: 200,
                data: getData,
            })
        } catch (error) {
            res.status(500).json('获取失败')
        }
    }

    /**
     * @description: 发布通知
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async announce (req, res) {
        console.log(req.body);
        let title = req.body.title
        let content = req.body.content
        let class_id = req.body.class_id
        class_id = class_id.split(';')
        try {
            let result = await this.announceMod(title, content)
            for (let i of class_id) {
                await this.announceClassMod(i, result.insertId)
            }
            res.json({
                msg: '发布成功',
                code: 200
            })
        } catch (error) {
            res.status(404).json('服务器出现错误')
        }
    }

    /**
     * @description: 获取所有通知与数量(分页获取)
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async getAllNotice (req, res) {
        try {
            let data = await this.getAllNoticeMod()
            res.json({
                data,
            })
        } catch (error) {
            res.json(err)
        }
    }

    /**
     * @description: 获取所有班级
     * @param {*}
     * @return {*}
     */
    static async getClass (req, res) {
        const result = await this.getClassMod()
        res.json(result)
    }
    static async delNotice (req, res) {
        try {
            const result = await this.delNoticeMod(req.query.n_id)
            if (result) {
                return res.json({
                    msg: '删除成功',
                    code: 200
                })
            }

        } catch (error) {
            res.json(error)
        }


    }

    static async NoticeDetails (req, res) {
        //1.获取公告id并查询
        //SELECT *FROM notice WHERE n_id=19;
        const data = await this.NoticeDetailsMod(req.query.n_id)
        //2.查看公告发给了哪个班级
        const class_id = await this.NoticeDetailsForClassMod(req.query.n_id)
        //3.统计人数
        let total = 0
        for (let i of class_id) {
            /*   console.log(i); */
            total += await this.NoticeDetailsTotalMod(i.class_id)
        }
        //4.阅读人数
        const readUser = await this.NoticeDetailsReadMod(req.query.n_id)
        res.json({
            msg: '获取成功',
            code: 200,
            data,
            class_id,
            total,
            readUser,
            readNum: readUser.length
        })
    }

    /**
     * @description: 获取班级
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async getClasses (req, res) {
        try {
            const result = await this.getClassesMod()
            res.json(result)
        } catch (error) {
            res.status(404).json(error)
        }

    }
    /**
     * @description: 添加班级
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async addClasses (req, res) {
        try {
            const result = await this.addClassesMod(req.body.c_id, req.body.classes)
            res.json(result)
        } catch (error) {
            res.status(404).json(error)
        }
    }

    /**
     * @description: 搜索班级
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async searchClasses (req, res) {
        try {
            const result = await this.searchClassesMod(req.query.c_id, req.query.classes)
            res.json(result)
        } catch (error) {
            res.status(404).json(error)
        }
    }

    /**
     * @description: 获取请假条
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async getuserLeave (req, res) {
        try {
            const result = await this.getuserLeaveMod(req.user.id)
            res.json(result)
        } catch (error) {
            res.status(404).json('服务器出现错误')
        }

    }

    static async setLeaveState (req, res) {
        try {
            await this.setLeaveStateMod(req.query.l_id, req.query.state)
            res.json({
                msg: '修改成功',
                code: 200
            })
        } catch (error) {
            res.status(404).json('服务器出现错误')
        }
    }

}